package com.vm.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.vm.activity.LoginActivity;
import com.vm.activity.RechargePhoneActivity;
import com.vm.activity.ShakeActivity;
import com.vm.activity.SignActivity;
import com.vm.commons.CurrentUser;
import com.wwtscapp.apk.R;

@SuppressLint("InflateParams")
public class ActiveFragment extends BaseFragment {
	RelativeLayout rlayout_exchange;
	RelativeLayout rlayout_sign;
	RelativeLayout rlayout_shake;
	private View view;
	/** 标志位，标志已经初始化完成 */
	private boolean isPrepared;
	/** 是否已被加载过一次，第二次就不再去请求数据了 */
	private boolean mHasLoadedOnce;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		Log.i("ManagerFragment", "onCreateView");

		if (view == null) {
			view = inflater.inflate(R.layout.activity, null);
		} else {
			// 缓存的rootView需要判断是否已经被加过parent
			// 如果有parent需要从parent删除
			ViewGroup parent = (ViewGroup) view.getParent();
			if (parent != null) {
				parent.removeView(view);
			}
		}
		view.setVisibility(View.INVISIBLE);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		isPrepared = true;
		lazyLoad();

	}

	public void initView() {
		rlayout_exchange = (RelativeLayout) view
				.findViewById(R.id.rlayout_exchange);
		rlayout_exchange.setOnClickListener(rlayout_ClickListener);
		rlayout_sign = (RelativeLayout) view.findViewById(R.id.rlayout_sign);
		rlayout_sign.setOnClickListener(rlayout_ClickListener);

		rlayout_shake = (RelativeLayout) view.findViewById(R.id.rlayout_shake);
		rlayout_shake.setOnClickListener(rlayout_ClickListener);
	}

	OnClickListener rlayout_ClickListener = new OnClickListener() {

		@SuppressLint("ShowToast")
		@Override
		public void onClick(View v) {
			if (v == rlayout_sign) {
				Intent intent = new Intent(getActivity(), SignActivity.class);
				startActivity(intent);
			} else if (v == rlayout_shake) {
				Intent intent = new Intent(getActivity(), ShakeActivity.class);
				startActivity(intent);
			} else if (v == rlayout_exchange) {
				if (CurrentUser.USERID != null) {
					Intent intent = new Intent(getActivity(),
							RechargePhoneActivity.class);
					startActivity(intent);
				} else {
					Toast toast = Toast.makeText(getActivity(), "请先登录", 500);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					Intent intent = new Intent(getActivity(),
							LoginActivity.class);
					startActivity(intent);
				}
			}
		}
	};

	@Override
	protected void lazyLoad() {
		// TODO Auto-generated method stub
		if (!isPrepared || !isVisible || mHasLoadedOnce) {
			return;
		}
		Log.i("FoundFragment", "lazyLoad--AsyncTask");
		new AsyncTask<Void, Void, Boolean>() {

			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				// 显示加载进度对话框
				UIHelper.showDialogForLoading(getActivity(), "正在加载...", true);
				mainHandler.sendMessage(new Message());
			}

			@Override
			protected Boolean doInBackground(Void... params) {
				try {
					Thread.sleep(1000);
					// 在这里添加调用接口获取数据的代码
					// doSomething()
				} catch (Exception e) {
					e.printStackTrace();
				}
				return true;
			}

			@Override
			protected void onPostExecute(Boolean isSuccess) {
				if (isSuccess) {
					// 加载成功

					mHasLoadedOnce = true;
				} else {
					// 加载失败
				}
				// 关闭对话框
				UIHelper.hideDialogForLoading();
				view.setVisibility(View.VISIBLE);
			}
		}.execute();
	}

	@SuppressLint("HandlerLeak")
	private Handler mainHandler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			initView();
		};
	};

}
